# Top-level Makefile for the GNU Compiler Collection for ARM/RISC OS
# Copyright (c) 2000, 2001 Nick Burrett
# Written by Nick Burrett <nick@dsvr.net>

# Set by the configure script
prefix=/home/riscos/cross
exec_prefix=${prefix}
datadir=${prefix}/share
includedir=${prefix}/include
infodir=${prefix}/info
libdir=${exec_prefix}/lib
libexecdir=${exec_prefix}/libexec
localstatedir=${prefix}/var
bindir=${exec_prefix}/bin
mandir=${prefix}/man
oldincludedir=/usr/include
sbindir=${exec_prefix}/sbin
sharedstatedir=${prefix}/com
srcdir=.
sysconfdir=${prefix}/etc

TARGET_NAME=arm-riscos-aof
TARGET_DEFS=-DTARGET_RISCOSAOF

# The native C and C++ compiler on the host computer
CC=gcc
CXX=g++
CFLAGS=-g -O2 $(TARGET_DEFS)
CXXFLAGS=-g -O2 $(TARGET_DEFS)

BISON=bison
FLEX=flex
GREP=grep
GPERF=gperf
MAKEINFO=makeinfo
SED=sed

# Installation directory of Unix binaries i.e. the stage 1 compiler.
ux_gccpkg=/home/riscos/cross

# Installation directory of RISC OS binaries i.e. the stage 2 compiler.
ro_gccpkg=/home/riscos//!gcc
ro_syspkg=/home/riscos//!System
ro_unixlibpkg=/home/riscos//!unixlib
ro_libsclpkg=/home/riscos//!clib

# Installation directory of RISC OS distribution packaged as zip files.
ro_zippkg=/home/riscos/riscos-pkg

# Suffix applied to executables (usually changed with --enable-filetype-ff8)
exesfx=,ff8

# Working directory where object files are stored
objdir=/usr/src/gccsdk-295/$(TARGET_NAME)

# Used in the GCC Makefiles and configuration files use these
hostsystem=HOST_ARM_LINUX
targetsystem=HOST_ARM_RISCOS

# GCC compiler version that we are going to build.
VERSION = 2_95_4

# Define the installation base directory.  These don't determine the actual
# stored location of files, rather they are the hard-coded values that
# the gcc driver and pre-processor use to find the binaries and default
# headers
unix_prefix=$(ux_gccpkg)/
riscos_prefix=/gccpkg:

# Generic installation directories
# For building the gcc and cpp binaries, unix_prefix or riscos_prefix
# are prepended to these paths.
# For installing the software, ux_gccpkg or ro_gccpkg are prepended to
# these paths.
gn_standard_exec_prefix=bin/
gn_gcc_bin_dir=bin/$(TARGET_NAME)/$(VERSION)
gn_standard_startfile_prefix=startup/
gn_gcc_include_dir=lib/gcc-lib/$(TARGET_NAME)/$(VERSION)/include
gn_gplusplus_include_dir=lib/gcc-lib/$(TARGET_NAME)/$(VERSION)/include/g++-3
gn_cross_include_dir=include
gn_local_include_dir=local/include
gn_system_include_dir=sys/include

# Used by Makefiles that build libraries/binaries for RISC OS.  These point
# to the installation directories for cross-host and RISC OS.
ux_bin_dir = $(ux_gccpkg)/$(gn_standard_exec_prefix)
ux_gcc_bin_dir = $(ux_gccpkg)/$(gn_gcc_bin_dir)
ro_bin_dir = $(ro_gccpkg)/$(gn_standard_exec_prefix)
ro_gcc_bin_dir = $(ro_gccpkg)/$(gn_gcc_bin_dir)
info_dir = $(ro_gccpkg)/docs
unixlib_incl_dir = /usr/src/gccsdk-295/unixlib/source/clib
libscl_incl_dir = /usr/src/gccsdk-295/libscl

# After we have build the stage 1 cross-compiler, then we can use these
# to build the cross-libraries and cross-tools
CROSS_CC=$(ux_gccpkg)/$(gn_standard_exec_prefix)/gcc
CROSS_CXX=$(ux_gccpkg)/$(gn_standard_exec_prefix)/g++
CROSS_CFLAGS=-O2 $(TARGET_DEFS) -mpoke-function-name
CROSS_CXXFLAGS=-O2 $(TARGET_DEFS) -mpoke-function-name

# Name of the host computers archiver.  This is only used to build
# libiberty or linking against the cross-compiler.
AR = ar
AR_FLAGS = -rc

# Name of the RISC OS archiver.  Used to build all RISC OS libraries.
CROSS_AR = $(ux_gccpkg)/$(gn_standard_exec_prefix)/libfile
CROSS_AR_FLAGS = -c

CROSS_LINK = $(ux_gccpkg)/$(gn_standard_exec_prefix)/drlink

insthdr = perl /usr/src/gccsdk-295/scripts/install-headers.pl
instsrc = perl /usr/src/gccsdk-295/scripts/install-source.pl

# Place variables in the environment.  This saves us having to pass
# the variables to sub-makes on the command line.
export TARGET_NAME objdir hostsystem targetsystem
export ux_gccpkg ro_gccpkg ro_syspkg ro_zippkg unix_prefix riscos_prefix
export gn_standard_exec_prefix gn_standard_startfile_prefix
export gn_gcc_include_dir gn_gplusplus_include_dir
export gn_local_include_dir gn_system_include_dir gn_cross_include_dir
export gn_gcc_bin_dir info_dir exesfx ro_unixlibpkg ro_libsclpkg
export ux_bin_dir ux_gcc_bin_dir ro_bin_dir ro_gcc_bin_dir
export unixlib_incl_dir libscl_incl_dir
export CC CXX
export CROSS_CC CROSS_CXX CROSS_CFLAGS CROSS_CXXFLAGS
export CROSS_AR CROSS_AR_FLAGS CROSS_LINK
export VERSION TARGET_NAME CFLAGS CXXFLAGS AR AR_FLAGS
export BISON FLEX GPERF GREP MAKEINFO SED
export insthdr instsrc

all:	build-stg1 build-stg2 build-stg3 build-stg4 build-stg5 build-stg6 \
	build-stg7

# Setup build and installation directories.  Install run-time library
# headers
setup:	directories install_headers

# Tools that will run natively in any environment
# i.e. under Linux this will build the GCC cross-compiler, cross-assembler
# and cross-linker.  Under RISC OS this will build the compiler, assembler
# and linker natively

# These dependencies are split to help make get parallel building right.
build-stg1:	libiberty-stg1
build-stg2:	build-stg1 riscos-aof-stg1
build-stg3:	build-stg2 gcc-stg1 cross-progs

# These targets build the libraries and binaries suitable for execution
# on RISC OS machines only.
build-stg4:	build-stg3 libiberty-stg2 libgcc unixlib
build-stg5:	build-stg4 gcc-stg2
build-stg6:	build-stg5 libscl libio libstdc riscos-aof-stg2
build-stg7:	build-stg6 libf2c libgpc riscos-libs riscos-progs riscos-dist

.PHONY: directories
.PHONY: c-libs
.PHONY: libgcc
.PHONY: libgpc
.PHONY: unixlib
.PHONY: riscos-progs
.PHONY: riscos-libs
.PHONY: riscos-elf
.PHONY: cross-progs
.PHONY: riscos-dist

.PHONY: libscl
.PHONY: gcc-stg1
.PHONY: gcc-stg2
.PHONY: libiberty-stg1
.PHONY: libiberty-stg2
.PHONY: riscos-aof-stg1
.PHONY: riscos-aof-stg2

.PHONY: build-stg1
.PHONY: build-stg2
.PHONY: build-stg3
.PHONY: build-stg4
.PHONY: build-stg5
.PHONY: build-stg6
.PHONY: build-stg7

.PHONY: install_headers

# Target (trgXX) and compiler flags (cflXX) definitions.  The XX must
# match for both trg and cfl in all calls to sub-make processes.  We
# write them like this because it makes the rest of the makefile easy
# to read.
trg10=apcs26/unixlib
trg11=apcs26/libscl

trg20=apcs32/arch3/unixlib
trg21=apcs32/arch4/unixlib

trg30=apcs32/arch3/libscl
trg31=apcs32/arch4/libscl

cfl10="CFL=-mapcs-26 -mcpu=arm2 -munixlib"
cfl11="CFL=-mapcs-26 -mcpu=arm2 -mlibscl"

cfl15="CFL=-mapcs-32 -mcpu=arm6 -mlibscl"

cfl20="CFL=-mapcs-32 -mcpu=arm6 -munixlib"
cfl21="CFL=-mapcs-32 -mcpu=strongarm -munixlib"

cfl30="CFL=-mapcs-32 -mcpu=arm6 -mlibscl"
cfl31="CFL=-mapcs-32 -mcpu=strongarm -mlibscl"

libiberty-stg1:
	$(MAKE) -C libiberty "STAGE=stage1"

libiberty-stg2:
	$(MAKE) -C libiberty "STAGE=stage2" "TRG=$(trg20)"

gcc-stg1:
	$(MAKE) -C gcc "STAGE=stage1" "TRG=$(trg20)"

gcc-stg2:
	$(MAKE) -C gcc "STAGE=stage2" "TRG=$(trg20)"

riscos-aof-stg1:
	$(MAKE) -C riscos-aof "STAGE=stage1"

riscos-aof-stg2:
	$(MAKE) -C riscos-aof "STAGE=stage2" "TRG=$(trg20)"

riscos-progs:
	$(MAKE) -C riscos-progs "TRG=$(trg20)" $(cfl20) "STAGE=stage2"

cross-progs:
	$(MAKE) -C cross-progs "STAGE=stage1"

riscos-elf:
	$(MAKE) -C riscos-elf

# The ballache of having to keep support with Objasm forces us
# to do this (CFL1) brain damage.
#unixlib:
#	$(MAKE) -C $@ "TRG=$(trg20)" $(cfl20)
c-libs: unixlib/Makefile unixlib/source/Makefile unixlib/source/object-list unixlib/objs/Makefile,fe1
	$(MAKE) -C unixlib "TRG=$(trg20)" $(cfl20)
	$(MAKE) -C libscl "TRG=$(trg30)" $(cfl30)

riscos-libs:
	$(MAKE) -C $@ "TRG=$(trg20)" $(cfl20)

libgcc:
	$(MAKE) -C gcc $@ "TRG=$(trg20)" $(cfl20)
	$(MAKE) -C gcc $@ "TRG=$(trg30)" $(cfl30)

libgpc libio libf2c libstdc:
	$(MAKE) -C gcc $@ "TRG=$(trg20)" $(cfl20)

riscos-dist:
	$(MAKE) -C riscos-dist install

riscos-pkg:
	$(MAKE) -C riscos-dist package

# Make sure build and installation directories exist
directories:
	mkdir -p $(ux_gccpkg)/$(gn_standard_exec_prefix)
	mkdir -p $(ux_gccpkg)/$(gn_standard_startfile_prefix)
	mkdir -p $(ux_gccpkg)/$(gn_gcc_bin_dir)
	mkdir -p $(ux_gccpkg)/$(gn_gcc_include_dir)
	mkdir -p $(ux_gccpkg)/$(gn_gplusplus_include_dir)
	mkdir -p $(ux_gccpkg)/$(gn_cross_include_dir)

	mkdir -p $(ro_gccpkg)/$(gn_standard_exec_prefix)
	mkdir -p $(ro_gccpkg)/$(gn_standard_startfile_prefix)
	mkdir -p $(ro_gccpkg)/$(gn_gcc_bin_dir)
	mkdir -p $(ro_gccpkg)/$(gn_gcc_include_dir)/h
	mkdir -p $(ro_gccpkg)/$(gn_gplusplus_include_dir)/h
	mkdir -p $(ro_gccpkg)/$(gn_cross_include_dir)

	mkdir -p $(ux_gccpkg)/$(gn_gcc_bin_dir)/$(trg10)
	mkdir -p $(ux_gccpkg)/$(gn_gcc_bin_dir)/$(trg11)
	mkdir -p $(ux_gccpkg)/$(gn_gcc_bin_dir)/$(trg20)
	mkdir -p $(ux_gccpkg)/$(gn_gcc_bin_dir)/$(trg21)
	mkdir -p $(ux_gccpkg)/$(gn_gcc_bin_dir)/$(trg30)
	mkdir -p $(ux_gccpkg)/$(gn_gcc_bin_dir)/$(trg31)

	mkdir -p $(ro_gccpkg)/$(gn_gcc_bin_dir)/$(trg10)/o
	mkdir -p $(ro_gccpkg)/$(gn_gcc_bin_dir)/$(trg11)/o
	mkdir -p $(ro_gccpkg)/$(gn_gcc_bin_dir)/$(trg15)/o
	mkdir -p $(ro_gccpkg)/$(gn_gcc_bin_dir)/$(trg20)/o
	mkdir -p $(ro_gccpkg)/$(gn_gcc_bin_dir)/$(trg21)/o
	mkdir -p $(ro_gccpkg)/$(gn_gcc_bin_dir)/$(trg30)/o
	mkdir -p $(ro_gccpkg)/$(gn_gcc_bin_dir)/$(trg31)/o

	mkdir -p ${ro_syspkg}/310/Modules
	mkdir -p $(info_dir)
	mkdir -p $(objdir)
	mkdir -p $(ro_zippkg)

	mkdir -p $(ro_unixlibpkg)/source/clib/o
	mkdir -p $(ro_libsclpkg)/o

# Install header files for our standard C run-time libraries.
install_headers:
	$(MAKE) -C libscl install_headers
	$(MAKE) -C unixlib install_headers
	$(MAKE) -C gcc/gcc -f Makelibs install_headers
	$(MAKE) -C gcc/libio install_headers
	$(MAKE) -C gcc/libstdc install_headers


# Generate unixlib makefiles
# Regenerate whenever Changelog is updated, so that any new files added will get added to the makefiles
unixlib/source/Makefile unixlib/source/object-list unixlib/objs/Makefile,fe1: unixlib/Makefile

unixlib/Makefile: unixlib/Makefile.in unixlib/source/Makefile.in unixlib/Docs/Changelog
	(cd unixlib; ./gen-makefiles.pl --dynamicdeps --stubs --objdir $(objdir)/unixlib/$(trg20))

clean:
	find arm-riscos-aof -type f | xargs rm

bin-clean:
	find $(ro_gccpkg) -type f | grep ,ff8 | xargs rm

